#pragma once

#include "my_defs.h"
#include "my_syscall.h"

/* accurate to microseconds */

#define HRTIME_SECOND                   (1000 * HRTIME_MSECOND)
#define HRTIME_MSECOND                  (1000 * HRTIME_USECOND)
#define HRTIME_USECOND                  (1LL)

static inline int64_t
my_usec_2_msec(int64_t usec)
{
    return usec / HRTIME_MSECOND;
}

static inline int64_t
my_usec_2_sec(int64_t usec)
{
    return usec / HRTIME_SECOND;
}

static inline int64_t
my_sec_2_usec(int64_t sec)
{
    return sec * HRTIME_SECOND;
}

static inline int64_t
my_msec_2_usec(int64_t msec)
{
    return msec * HRTIME_MSECOND;
}

static inline int64_t
my_ts_2_usec(const my_timespec_t *ts)
{
    return my_sec_2_usec(ts->tv_sec) + (int64_t)(ts->tv_nsec / 1000);
}

static inline void
my_usec_2_ts(int64_t usec, my_timespec_t *ts)
{
    ts->tv_sec = my_usec_2_sec(usec);
    ts->tv_nsec = (usec % HRTIME_SECOND) * 1000;
}

/*
 * get_cache_time: return global cache time
 * @return: usec
 */
int64_t get_cache_time();

/*
 * get_latest_time: use clock_gettime to get latest time and update g_cache_time
 * @return: nsecond
 */
int64_t get_latest_time();

#define MY_HRTIME_HTTP_TIME_SIZE                    sizeof("Mon, 28 Sep 1970 06:00:00 GMT")
#define MY_LOG_FILE_TIME_SIZE                       sizeof("2021/10/10 10:10:10")

int64_t my_http_time(char *buf, int64_t buf_size);

int64_t my_log_file_time(char *buf, int64_t buf_size);
